#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    camera_socket=new QTcpSocket();

    //slots:on_connected();
    connect(camera_socket,&QTcpSocket::connected,this,&MainWindow::on_connected);
    connect(camera_socket,&QTcpSocket::readyRead,this,&MainWindow::on_recv_camera);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    camera_socket->connectToHost("192.168.50.248",8888);
}


void  MainWindow::on_recv_camera()
{
     if(flag==0)
     {
         if(camera_socket->bytesAvailable()<4)
         {
             return ;
         }
         camera_socket->read((char*)&length,4);
         qDebug()<<length;

         flag=1;
     }
     else{
         if(camera_socket->bytesAvailable()<length)
         {
             return;
         }
         uchar buf[1024*1024];
         camera_socket->read((char*)buf,length);
         qDebug()<<"已读取";
         //图片保存
         QPixmap pic;
         pic.loadFromData(buf,length);
         ui->label->setPixmap(pic);
         ui->label->setScaledContents(true);//获取更精细
         flag=0;
     }
}


void MainWindow::on_pushButton_2_clicked()
{
    camera_socket->close();
    qDebug()<<"已关闭";
}


